package com.wly
{
	import com.wly.utils.PNGEncoder;
	
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.system.fscommand;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	public class BlockCreaterMain extends Sprite
	{
		private var _tfUint:TextField;
		private var _tfBlockMaxCount:TextField;
		private var _btnCreate:SimpleButton;
		private var _tmxFileName:TextField;
		private var _mapWidth:TextField;
		private var _mapHeight:TextField;
		
		public function BlockCreaterMain()
		{
			super();
			
			_tfUint = getChildByName("tfCellUint") as TextField;
			_tfBlockMaxCount = getChildByName("tfMaxCount") as TextField;
			_tmxFileName     = getChildByName("tfFileName") as TextField;
			_mapWidth        = getChildByName("tfMapWidth") as TextField;
			_mapHeight       = getChildByName("tfMapHeight") as TextField;
			
			_mapWidth.restrict = "0-9";
			_mapHeight.restrict = "0-9";
			_tfUint.restrict = "0-9";
			_tfBlockMaxCount.restrict = "0-9";
			_btnCreate = getChildByName("btnCreate") as SimpleButton;
			_btnCreate.addEventListener(MouseEvent.CLICK, createBlock);
		}
		
		private function createBlock(e:MouseEvent):void
		{
			var cellUint:int = parseInt(_tfUint.text);
			var maxBlockNum:int = parseInt(_tfBlockMaxCount.text);
			createBlocks(cellUint, maxBlockNum);
			saveTmxInfo(_tmxFileName.text+".tmx", cellUint, maxBlockNum);
		}
		
		public function createBlocks(cellUint:int, maxUintNum:int):void
		{
			var i:int;
			var j:int;
			var sp:Sprite;
			var fileName:String;
			var bmd:BitmapData;
			
			for(i=1; i<=maxUintNum; i++)
			{
				for(j=1; j<=maxUintNum; j++)
				{
					sp = new Sprite;
					sp.graphics.beginFill(0xcccc33);
					sp.graphics.drawRect(0, 0, i*cellUint, j*cellUint);
					sp.graphics.endFill();
					with(sp.graphics)
					{
						lineStyle(3, 0x000000, 2);
						moveTo(0, 0);
						lineTo(0, j*cellUint);
						moveTo(0, j*cellUint);
						lineTo(i*cellUint, j*cellUint);
						moveTo(i*cellUint, j*cellUint);
						lineTo(i*cellUint, 0);
						moveTo(i*cellUint, 0);
						lineTo(0,0);
					}
					
					fileName = (i*cellUint).toString()+"-"+(j*cellUint).toString()+".png";
					bmd = new BitmapData(i*cellUint, j*cellUint);
					bmd.draw(sp);
					saveBitMap(bmd, fileName);
				}
			}
		}
		
		private function saveBitMap(bmd:BitmapData, fileName:String):void
		{
			if(!bmd)
			{
				return;
			}
			
			var byteArr:ByteArray = PNGEncoder.encode(bmd);
			var file:File = new File(File.applicationDirectory.resolvePath(fileName).nativePath);
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.WRITE);
			fs.writeBytes(byteArr);
			fs.close();
		}
		
		private function saveTmxInfo(fileName:String, cellUint:int, maxUintNum:int):void
		{
			var xml:XML = <map version="1.0" orientation="orthogonal">
				</map>;
			xml.@width  = parseInt(_mapWidth.text);
			xml.@height = parseInt(_mapHeight.text);
			xml.@tilewidth  = cellUint.toString();
			xml.@tileheight = cellUint.toString();
			
			var i:int;
			var j:int;
			var block:XML;
			var img:XML;
			var offset:XML;
			
			for(i=1; i<=maxUintNum; i++)
			{
				for(j=1; j<=maxUintNum; j++)
				{
					block = <tileset>
							</tileset>;
					block.@firstgid  = i*maxUintNum+j;
					block.@name      = (i*cellUint).toString()+"-"+(j*cellUint).toString();
					block.@tilewidth = (i*cellUint).toString();
					block.@tileheight= (j*cellUint).toString();
					offset = <tileoffset/>;
					offset.@x = "0";
					offset.@y = ((j-1)*cellUint).toString();
					img = <image/>;
					img.@source = (i*cellUint).toString()+"-"+(j*cellUint).toString()+".png";
					img.@width  = (i*cellUint).toString();
					img.@height = (j*cellUint).toString();
					
					block.appendChild(offset);
					block.appendChild(img);
					xml.appendChild(block);
				}
			}
			
			var file:File = new File(File.applicationDirectory.resolvePath(fileName).nativePath);
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.WRITE);
			fs.writeUTFBytes('<?xml version="1.0" encoding="UTF-8"?>\n');
			fs.writeUTFBytes(xml.toString());
			fs.close();
		}
	}
}